package com.weihan.javautils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ConcatenateText {
    public static void main(String[] args) {

        if(args.length<2){
            System.out.println("Missing arguments!! ");
            return;
        }
        File inputDir = new File(args[0]);
        if(!inputDir.isDirectory() || !inputDir.exists()){
            System.out.println("Incorrect input directory");
            return;
        }
        File outputFile = new File(args[1]);
        if(!outputFile.isFile()){
            System.out.println("Incorrect output file");
            return;
        }

        List<String> fileslist = getAllFile(args[0],false);
        mergeFiles(args[1],fileslist.toArray(new String[fileslist.size()]));
    }

    /**
     * 获取路径下的所有文件/文件夹
     * @param directoryPath 需要遍历的文件夹路径
     * @param isAddDirectory 是否将子文件夹的路径也添加到list集合中
     * @return
     */
    public static List<String> getAllFile(String directoryPath,boolean isAddDirectory) {
        List<String> list = new ArrayList<String>();
        File baseFile = new File(directoryPath);
        if (baseFile.isFile() || !baseFile.exists()) {
            return list;
        }
        File[] files = baseFile.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                if(isAddDirectory){
                    list.add(file.getAbsolutePath());
                }
                list.addAll(getAllFile(file.getAbsolutePath(),isAddDirectory));
            } else {
                list.add(file.getAbsolutePath());
            }
        }
        return list;
    }

    public static final int BUFSIZE = 1024 * 8;

    /**
     * 合并文件
     * @param outFile 输出路径
     * @param files 输入路径
     */
    public static void mergeFiles(String outFile, String[] files) {
        FileChannel outChannel = null;
        System.out.println("Merge " + Arrays.toString(files) + " into " + outFile);
        try {
            outChannel = new FileOutputStream(outFile).getChannel();
            for(String f : files){
                if(f.equals(outFile))return;
                FileChannel fc = new FileInputStream(f).getChannel();
                ByteBuffer bb = ByteBuffer.allocate(BUFSIZE);
                ByteBuffer bb2 = ByteBuffer.allocate(BUFSIZE);
                String str = "\r\n\r\n*****\r\n"+new File(f).getName()+"\r\n*****\r\n\r\n";
                bb2.put(str.getBytes("UTF-8"));
                bb2.flip();
                outChannel.write(bb2);

                while(fc.read(bb) != -1){
                    bb.flip();
                    outChannel.write(bb);
                    bb.clear();
                }
                fc.close();
            }
            System.out.println("Merged!! ");
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {if (outChannel != null) {outChannel.close();}} catch (IOException ignore) {}
        }
    }



}
